home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / GDIMETA.PAK / FILEDLG.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  6KB  |  200 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE: filedlg.c
  9. //
  10. //  PURPOSE: Shows basic use of "Open" and "Save As" common dialogs.
  11. //
  12. //
  13. //  FUNCTIONS:
  14. //
  15. //
  16. //  COMMENTS:
  17. //
  18. //
  19. //
  20. //  SPECIAL INSTRUCTIONS: N/A
  21. //
  22.  
  23. #include <windows.h>            // required for all Windows applications
  24. #include <windowsx.h>
  25. #include <commctrl.h>
  26. #include "globals.h"            // prototypes specific to this application
  27. #include "resource.h"
  28.  
  29. static char szDirName[256] = {'\0'};    // directory string
  30. void SetDirectory(LPOPENFILENAME);
  31.  
  32. //
  33. //  FUNCTION: CmdFileOpen(HWND, WORD, WORD, HWND)
  34. //
  35. //  PURPOSE: Call the open common dialog and show its results.
  36. //
  37. //  PARAMETERS:
  38. //    hwnd     - The window handle.
  39. //    wCommand - IDM_OPEN (Unused)
  40. //    wNotify  - (Unused)
  41. //    hwndCtrl - NULL (Unused)
  42. //
  43. //  RETURN VALUE:
  44. //
  45. //  COMMENTS:
  46. //
  47. //
  48.  
  49. #pragma argsused
  50. LRESULT CmdFileOpen(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl) {
  51.     OPENFILENAME ofn = {0}; // common dialog box structure
  52.     char szFile[256];       // filename string
  53.     char szFileTitle[256];  // file-title string
  54.     char szFilter[256];     // filter string
  55.     char szDefExt[32];      // default file extension
  56.     char chReplace;         // strparator for szFilter
  57.     int i, cbString;        // integer count variables
  58.  
  59.     if (!QuerySaveFile(hwnd))
  60.         return(0);
  61.  
  62.     // Retrieve the current directory name and store it in szDirName.
  63.  
  64.     if (szDirName[0] == '\0')
  65.         GetCurrentDirectory(sizeof(szDirName), szDirName);
  66.  
  67.     // Place the terminating null character in the szFile.
  68.  
  69.     szFile[0] = '\0';
  70.  
  71.     // Load the filter string from the resource file.
  72.  
  73.     cbString = LoadString(hInst, IDS_OPENFILTER, szFilter, sizeof(szFilter));
  74.  
  75.     // Add a terminating null character to the filter string.
  76.  
  77.     chReplace = szFilter[cbString - 1];
  78.     for (i = 0; szFilter[i] != '\0'; i++)
  79.     {
  80.         if (szFilter[i] == chReplace)
  81.             szFilter[i] = '\0';
  82.     }
  83.  
  84.     // Load the default file extension string
  85.  
  86.     LoadString(hInst, IDS_DEFEXT, szDefExt, sizeof(szDefExt));
  87.  
  88.     // Set the members of the OPENFILENAME structure.
  89.  
  90.     ofn.lStructSize     = sizeof(OPENFILENAME);
  91.     ofn.hwndOwner       = hwnd;
  92.     ofn.lpstrFilter     = szFilter;
  93.     ofn.nFilterIndex    = 1;
  94.     ofn.lpstrFile       = szFile;
  95.     ofn.nMaxFile        = sizeof(szFile);
  96.     ofn.lpstrFileTitle  = szFileTitle;
  97.     ofn.nMaxFileTitle   = sizeof(szFileTitle);
  98.     ofn.lpstrInitialDir = szDirName;
  99.     ofn.Flags = OFN_SHOWHELP | OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;
  100.     ofn.lpstrDefExt     = szDefExt;
  101.  
  102.     // Display the Open dialog box.
  103.  
  104.     if (GetOpenFileName(&ofn))
  105.     {
  106.         Open(ofn.lpstrFile,
  107.              FALSE,
  108.              hwnd);
  109.         SetDirectory(&ofn);
  110.     }
  111.     return 0;
  112. }
  113.  
  114.  
  115. //
  116. //  FUNCTION: CmdFileSaveAs(HWND, WORD, WORD, HWND)
  117. //
  118. //  PURPOSE: Call the SaveAs common dialog and show the results.
  119. //
  120. //  PARAMETERS:
  121. //    hwnd     - The window handle.
  122. //    wCommand - IDM_SAVEAS (Unused)
  123. //    wNotify  - (Unused)
  124. //    hwndCtrl - NULL (Unused)
  125. //
  126. //  RETURN VALUE:
  127. //
  128. //  COMMENTS:
  129. //
  130. //
  131.  
  132. #pragma argsused
  133. LRESULT CmdFileSaveAs(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl) {
  134.     OPENFILENAME ofn = {0}; // common dialog box structure
  135.     char szFile[256];       // filename string
  136.     char szFileTitle[256];  // file-title string
  137.     char szFilter[256];     // filter string
  138.     char szDefExt[32];      // default file extension
  139.     char chReplace;         // string separator for szFilter
  140.     int i, cbString;        // integer count variables
  141.  
  142.     // Retrieve the current directory name and store it in szDirName.
  143.  
  144.     if (szDirName[0] == '\0')
  145.         GetCurrentDirectory(sizeof(szDirName), szDirName);
  146.  
  147.     // Place the terminating null character in szFile.
  148.  
  149.     szFile[0] = '\0';
  150.  
  151.     // Load the filter string from the .RC file.
  152.  
  153.     cbString = LoadString(hInst, IDS_SAVEFILTER, szFilter, sizeof(szFilter));
  154.  
  155.      // Add a terminating null character to the filter string.
  156.  
  157.     chReplace = szFilter[cbString - 1];
  158.     for (i = 0; szFilter[i] != '\0'; i++)
  159.     {
  160.         if (szFilter[i] == chReplace)
  161.             szFilter[i] = '\0';
  162.     }
  163.  
  164.     // Load the default file extension string
  165.  
  166.     LoadString(hInst, IDS_DEFEXT, szDefExt, sizeof(szDefExt));
  167.  
  168.     // Set the members of the OPENFILENAME structure.
  169.  
  170.     ofn.lStructSize     = sizeof(OPENFILENAME);
  171.     ofn.hwndOwner       = hwnd;
  172.     ofn.lpstrFilter     = szFilter;
  173.     ofn.nFilterIndex    = 1;
  174.     ofn.lpstrFile       = szFile;
  175.     ofn.nMaxFile        = sizeof(szFile);
  176.     ofn.lpstrFileTitle  = szFileTitle;
  177.     ofn.nMaxFileTitle   = sizeof(szFileTitle);
  178.     ofn.lpstrInitialDir = szDirName;
  179.      ofn.Flags = OFN_SHOWHELP | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
  180.     ofn.lpstrDefExt     = szDefExt;
  181.  
  182.     // Display the Save As dialog box.
  183.  
  184.     if (GetSaveFileName(&ofn))
  185.     {
  186.         SaveAs(ofn.lpstrFile, hwnd);
  187.         SetDirectory(&ofn);
  188.         return 1;               // Indicate success
  189.     }
  190.  
  191.      return 0;
  192. }
  193.  
  194.  
  195. void SetDirectory(LPOPENFILENAME lpofn)
  196. {
  197.     lpofn->lpstrFile[lpofn->nFileOffset] = '\0';
  198.     lstrcpy(szDirName, lpofn->lpstrFile);
  199. }
  200.